using GameFramework;
using Unity.VisualScripting;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace LS.UGFUnits
{
    [UnitShortTitle("Config")]
    [UnitSurtitle("Add")]
    public sealed class AddConfig : ConfigUnit
    {
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput Key { get; private set; }

        [DoNotSerialize] 
        [PortLabelHidden]
        public ValueInput BoolValue { get; private set; }
        
        [DoNotSerialize] 
        [PortLabelHidden]
        public ValueInput IntValue { get; private set; }

        [DoNotSerialize] 
        [PortLabelHidden]
        public ValueInput FloatValue { get; private set; }
                
        [DoNotSerialize] 
        [PortLabelHidden]
        public ValueInput StringValue { get; private set; }
        
        protected override void Definition()
        {
            base.Definition();
            Key = ValueInput<string>(nameof(Key), string.Empty);
            BoolValue = ValueInput<bool>(nameof(BoolValue), false);
            IntValue = ValueInput<int>(nameof(IntValue), 0);
            FloatValue = ValueInput<float>(nameof(FloatValue), 0f);
            StringValue = ValueInput<string>(nameof(StringValue), string.Empty);
            
            Requirement(Key, Input);
            Requirement(BoolValue, Input);
            Requirement(IntValue, Input);
            Requirement(FloatValue, Input);
            Requirement(StringValue, Input);
        }

        protected override ControlOutput In(Flow flow)
        {
            if (Target == null)
            {
                Debug.LogError("GameFramework is not initialized.");
                return base.In(flow);
            }

            string key = flow.GetValue<string>(Key);
            string stringValue = flow.GetValue<string>(StringValue);
            int intValue = flow.GetValue<int>(IntValue);
            float floatValue = flow.GetValue<float>(FloatValue);
            bool boolValue = flow.GetValue<bool>(BoolValue);
            
            Target?.AddConfig(key, boolValue, intValue, floatValue, stringValue);
            return base.In(flow);
        }
    }
}